﻿//=============================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 作 者：江怀玉
// 日 期：2011-10-11
// 描 述：页面基类，所有的aspx页面继承自基类
//        页面基类提供常用的方法和常用的用户信息
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/10/11   江怀玉           
//=============================================================

using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Model;
using iSun.BLL.Base;

namespace iSun.Web.UI.Controls
{
    public class MenuListPage : BasePage
    {
        /// <summary>
        /// 获取数据数据条件
        /// </summary>
        public string Condition
        {
            get { return ""; }
        }

        /// <summary>
        /// 定义实体ID
        /// </summary>
        public override string EntityId
        {
            get
            {
                BaseEntity instance = iSun.Common.Reflection.CreateEntitysInstance(this.EntityName) as BaseEntity;
                if (instance != null)
                {
                    return instance.GetTableId();
                }
                return "ListId";
            }
        }

        public string Order
        {
            get
            {
                return GetStringFromQueryString("order", "");
            }
        }

        public string MasterEntityName
        {
            get
            {
                return GetStringFromQueryString("masterentity", "DatDepartment");
            }
        }
        public string conditonId
        {
            get
            {
                return GetStringFromQueryString("conditonid", "");
            }
        }
        public string selectId
        {
            get
            {
                return GetStringFromQueryString("selectid", "");
            }
        }
        public string pId
        {
            get
            {
                return GetStringFromQueryString("pid", "");
            }
        }

        /// <summary>
        /// Querystring formatter:?masterPara=SysFunction(tablename)|Name(Caption)|FunctionID(key)|ParentID(ParentKey)|FunctionPath
        /// </summary>
        public string MasterPara
        {
            get
            {
                return GetStringFromQueryString("masterPara", "");
            }
        }

        public string DetailEntityName
        {
            get;
            set;
        }
        public string ForeignKey
        {
            get;
            set;

        }

        private void SetTreeControl(System.Web.UI.Control ctrl)
        {
            foreach (System.Web.UI.Control c in ctrl.Controls)
            {
                if (c is TreeViewMenu)
                {
                    var tv1 = c as TreeViewMenu;
                    if (!string.IsNullOrEmpty(MasterPara))
                    {

                        string[] paras = MasterPara.Split('|');
                        if (paras.Length > 2)
                        {
                            tv1.TableName = paras[0];
                            tv1.Caption = paras[1];
                            tv1.Key = paras[2];
                            ForeignKey = tv1.Key;
                        }

                        if (paras.Length > 3 && tv1 != null)
                        {
                            tv1.AllowCheck = bool.Parse(paras[3]);
                        }
                    }
                    break;

                }
                else
                {
                    SetTreeControl(c);
                }
            }
        }
        protected override void OnLoad(System.EventArgs e)
        {
            SetTreeControl(this.Page);
        }
        /// <summary>
        /// 定义数据字段
        /// </summary>
        public string DataFields
        {
            get
            {
                return GetDataFields();
            }
        }

        /// <summary>
        /// 定义数据列
        /// </summary>
        public string DataColums
        {
            get
            {
                return GetColums();
            }
        }

        public int _width;
        /// <summary>
        ///编辑页面宽
        /// </summary>
        public int EditWindowWidth
        {
            get
            {
                if (_width < 500)
                {
                    return 500;
                }
                return _width;
            }
            set
            {
                if (_width != value && value > 500)
                {
                    _width = value;
                }
            }
        }

        public int _height;
        /// <summary>
        ///编辑页面高
        /// </summary>
        public int EditWindowHeight
        {
            get
            {
                if (_height < 500)
                {
                    return 500;
                }
                return _height;
            }
            set
            {
                if (_height != value && value > 500)
                {
                    _height = value;
                }
            }
        }


        /// <summary>
        /// 定义样式
        /// </summary>
        public string DataTableCss
        {
            get
            {
                return GetColumsCss();
            }
        }


        IList<DataListTemplate> list;
        /// <summary>
        /// 获取字段列
        /// </summary>
        /// <returns></returns>
        private string GetDataFields()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
            }
            string _strHtml = "key:'{0}',label:'{1}',sortable:true";
            if (list == null)
            {
                return ("{" + string.Format(_strHtml, "Not Data", "Not Data", 400) + "},");
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.OrderBy(p => p.iOrder);
            foreach (DataListTemplate _template in _list)
            {
                _sbHtml.Append("{" + string.Format(_strHtml, _template.Field, _template.THCaption, _template.Width) + "},");
            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }

        /// <summary>
        /// 获得表头
        /// </summary>
        /// <returns></returns>
        public string GetColums()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
            }
            if (list == null)
            {
                return "";
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.OrderBy(p => p.iOrder);
            foreach (DataListTemplate _template in _list)
            {
                _sbHtml.Append("'" + _template.Field + "',");
            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }

        /// <summary>
        /// 获得表头
        /// </summary>
        /// <returns></returns>
        public string GetColumsCss()
        {
            if (list == null)
            {
                DataListTemplate template = DataListTemplate.CreateInstance();
                BaseBussnies<DataListTemplate> bll = new BaseBussnies<DataListTemplate>(template);
                list = bll.GetEntityListByCondition<DataListTemplate>(" EntityName='" + EntityName + "'");
            }
            string _strHtml = ".yui-skin-sam .yui-dt-col-{0}{{width:{1}px}}\n";
            if (list == null)
            {
                return (string.Format(_strHtml, "Not Data", "Not Data", 400));
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = list.OrderBy(p => p.iOrder);
            foreach (DataListTemplate _template in _list)
            {
                _sbHtml.Append(_strHtml.Replace("{0}", _template.Field).Replace("{1}", _template.Width == 0 ? "100" : _template.Width.ToString() + ""));
            }
            _sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }
    }
}
